home *** CD-ROM | disk | FTP | other *** search
- From: SimsGW@msn.com (Gary Sims)
- Subject: RE: Function body banned in .H file - Can I still inline?
- Date: 14 Jan 96 21:42:20 -0800
- References: <4d0ir0$68e@newsroom.hitc.com>
- Message-ID: <00001a81+00008dc1@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c++
- Organization: The Microsoft Network (msn.com)
-
- I think you've misunderstood something you read, Chris. You can put
- the body of a function in the header file. That's the only practical
- way to have inline functions. What you can NOT do is present the body
- to the compiler more than once. Since the same header is frequently
- #include'd by one more than one module, you must prevent it being
- interpreted twice. Here's how you do that. I've included an example
- of the two ways to specify an inline function:
-
- #ifndef THIS_HEADER_NAME
- #define THIS_HEADER_NAME
-
- class Foo
- {
- int PrivateXValue;
- public:
- Foo();
- int GetX(){return PrivateXValue;};
- int GetFractionalX(const int Divisor);
- };
-
- int inline Foo::GetFractionalX(const int Divisor)
- {
- return PrivateXValue/Divisor;
- };
-
- #endif
-
- The first time the compiler sees this, the value THIS_HEADER_NAME
- will not have been defined, so the lines between there and the #endif
- will be processed. On successive occasions, it WILL be defined, so
- they won't. That avoids the problem of double definitions of the
- function body. Hope this helps,
-
- Gary W. Sims
- Stonehaven Laboratory
-